<template>
  <div class="wrap">
    <div class="search">
      <input type="text" v-model="supplierName" @keyup.enter="query" placeholder="名称搜索" />
      <img src="../assets/search.png" alt @click="query" />
    </div>
    <div class="tab">
      <p :class="tabIndex==1?'active':''" @click="setTabIndex(1)">供应商列表</p>
      <p :class="tabIndex==2?'active':''" @click="setTabIndex(2)">客户列表</p>
    </div>
    <div class="dateBox">
      <div class="lkbox">
        <div class="lk" @click="orderState = true">
          <p :class="sendStateTag==1?'act':''">{{sendStateName}}</p>
          <img :src="sendStateTag==1?require('../assets/top2.png'):require('../assets/zx.png')" alt />
        </div>
        <div class="lk" @click="show = true">
          <p :class="createUserIdState==1?'act':''">{{createUserIdName}}</p>
          <img
            :src="createUserIdState==1?require('../assets/top2.png'):require('../assets/zx.png')"
            alt
          />
        </div>
      </div>
      <div class="date" @click="dateState = true">
        <p>{{date?date:"日期筛选"}}</p>
        <img :src="date?require('../assets/top.png'):require('../assets/bot.png')" alt />
      </div>
    </div>
    <template v-if="tabIndex==1">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <div class="list" v-if="SupplierList.length">
          <div class="item" v-for="(item,idx) in SupplierList" :key="idx" @click="next(item)">
            <div class="left">
              <img :src="item.supplier_avatar" alt :onerror="defaultImg" />
              <div>
                <p>{{item.supplier_name}}</p>
                <span>订单数：{{item.supplier_order_count}}</span>
              </div>
            </div>
            <div class="phoneBox">
              <p>{{item.supplier_tel2}}</p>
              <img src="../assets/right.png" class="right" alt />
            </div>
          </div>
        </div>
        <van-empty description="暂无数据" v-if="state1==1" />
      </van-pull-refresh>
    </template>
    <template v-if="tabIndex==2">
      <van-pull-refresh v-model="refreshing2" @refresh="onRefresh2">
        <div class="list" v-if="CustomerList.length">
          <div class="item" v-for="(item,idx) in CustomerList" :key="idx" @click="detail(item)">
            <div class="left">
              <img :src="item.customer_avatar" alt />
              <div>
                <p>{{item.customer_name}}</p>
                <span>订单数：{{item.customer_order_count}}</span>
              </div>
            </div> 
            <div class="phoneBox">
              <p>{{item.customer_tel2}}</p>
              <img src="../assets/right.png" class="right" alt />
            </div>
          </div>
        </div>
        <van-empty description="暂无数据" v-if="state2==1" />
      </van-pull-refresh>
    </template>
    <van-action-sheet
      v-model="show"
      description="选择录入人"
      :actions="actions"
      :round="false"
      @select="onSelect"
    />
    <van-action-sheet
      v-model="orderState"
      description="选择订单状态"
      :round="false"
      :actions="sendState"
      @select="onOrder"
    />
    <van-popup v-model="dateState" position="bottom">
      <van-datetime-picker
        v-model="createDate"
        type="date"
        @confirm="dateConfirm"
        @cancel="dateCancel"
        cancel-button-text="全部"
      />
    </van-popup>
  </div>
</template>
<style>
.van-pull-refresh__track {
  height: 60vh;
  overflow-y: scroll;
}
</style>
<style lang="scss" scoped>
.null {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 80vh;
  p {
    color: #999;
    font-size: 0.48rem;
  }
}
.tab {
  display: flex;
  width: 100%;
  margin: 10px auto 0;
  border-bottom: 10px solid #fafafa;
  justify-content: space-around;
  p {
    font-size: 0.4rem;
    color: #555;
    line-height: 1.066667rem;
    width: 30%;
    text-align: center;
  }
  .active {
    color: #719bef;
    border-bottom: 3px solid #719bef;
  }
}
.search {
  background: #eeeded;
  height: 0.9rem;
  width: 9.2rem;
  margin: 0.306667rem auto 0.4rem;
  border-radius: 0.266667rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  input {
    border: none;
    outline: none;
    background: transparent;
    margin-left: 15px;
  }
  img {
    width: 0.533333rem;
    height: 0.533333rem;
    display: block;
    margin-right: 15px;
  }
}
.dateBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e5e5e5;
  padding-bottom: 0.4rem;
  width: 9.2rem;
  margin: 0.4rem auto 0;
  .date {
    padding: 0 0.5rem;
    height: 0.613333rem;
    background: #719bef;
    border-radius: 0.306667rem;
    margin-right: 0.4rem;
    display: flex;
    justify-content: space-around;
    font-size: 0.32rem;
    color: #fff;
    align-items: center;
    p:nth-of-type(2) {
      margin-left: 0.5rem;
    }
    img {
      width: 10px;
      margin-left: 4px;
    }
  }
  .lkbox {
    display: flex;
  }
  .lk {
    display: flex;
    align-items: center;
    margin-left: 0.4rem;
    img {
      width: 10px;
      margin-left: 4px;
      margin-top: 2px;
    }
    p {
      font-size: 0.32rem;
      color: #999;
    }
    .act {
      color: #719bef;
    }
  }
}
.item {
  height: 1.6rem;
  border-bottom: 1px solid #e5e5e5;
  width: 9.2rem;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    img {
      width: 1.013333rem;
      height: 1.013333rem;
      display: block;
      border-radius: 50%;
      margin-right: 0.186667rem;
    }
    p {
      color: #555;
      font-size: 0.4rem;
      text-align: left;
    }
    span {
      color: #999;
      font-size: 0.25rem;
      text-align: left;
      display: block;
    }
  }
  .phoneBox {
    display: flex;
    align-items: center;
  }
  .right {
    display: block;
    width: 0.4rem;
    height: 0.4rem;
    margin-left: 8px;
  }
}
.van-action-sheet {
  max-height: 50%;
  overflow-y: scroll;
}
</style>
<script>
import {
  getSupplierList,
  getCustomerList,
  getCreateUserOption,
  orderListAll,
  getUserInfo,
  findUserInfo
} from "@/utils/interface";
import { formatDate } from "@/utils/tools";
import { Empty } from "vant";

export default {
  components: {
    Empty
  },
  data() {
    return {
      defaultImg: 'this.src="' + require("../assets/user.png") + '"',
      tabIndex: 1,
      show: false,
      orderState: false,
      actions: [],
      sendState: [
        { name: "全部", value: "" },
        { name: "已到货", value: 1 },
        { name: "未到货", value: 0 }
      ],
      createDate: new Date(),
      dateState: false,
      SupplierList: [],
      CustomerList: [],
      date: "",
      supplierName: "",
      orderValue: "",
      user_id: "",
      refreshing: false,
      state1: 0,
      refreshing2: false,
      state2: 0,
      sendStateName: "所有状态",
      sendStateTag: 0,
      createUserIdName: "所有录入人",
      createUserIdState: 0,
      state: 0
    };
  },

  created() {
    document.title = "全部订单"
    let date = sessionStorage.getItem("index-date");
    date ? (this.date = date) : "";
    this.getSupplierList(
      this.supplierName,
      this.orderValue,
      this.date,
      this.user_id
    );
    this.getCustomerList(
      this.supplierName,
      this.orderValue,
      this.date,
      this.user_id
    );
    this.getCreateUserOption();
    this.findUserInfo();

    console.log();
  },
  methods: {
    onRefresh() {
      this.getSupplierList(this.supplierName);
    },
    onRefresh2() {
      this.getCustomerList(this.supplierName);
    },
    query() {
      // 供应商搜索
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
    },
    setTabIndex(index) {
      this.tabIndex = index;
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
    },
    next(item) {
      this.$router.push({
        path: "order",
        query: { info: JSON.stringify(item) ,state: this.orderValue,user_id:this.user_id,createUserIdName: this.createUserIdName}
      });
    },
    detail(item) {
      this.$router.push({
        path: "orderInfo",
        query: { info: JSON.stringify(item),state: this.orderValue,user_id:this.user_id,createUserIdName: this.createUserIdName }
      });
    },
    onSelect(item) {
      this.createUserIdName = item.name == "全部" ? "所有录入人" : item.name;
      this.user_id = item.value;
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      this.show = false;
    },
    onOrder(item) {
      this.sendStateName = item.name == "全部" ? "所有状态" : item.name;
      // 供应商搜索
      this.orderValue = item.value;
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      this.orderState = false;
    },
    // 筛选时间
    dateConfirm(e) {
      console.log(e);
      this.dateState = false;

      this.date = formatDate(e);
      sessionStorage.setItem("index-date", this.date);
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
    },
    dateCancel() {
      this.date = "";
      sessionStorage.setItem("index-date", "");
      if (this.tabIndex == 1) {
        this.getSupplierList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      // 客户搜索
      if (this.tabIndex == 2) {
        this.getCustomerList(
          this.supplierName,
          this.orderValue,
          this.date,
          this.user_id
        );
      }
      this.dateState = false;
    },
    // 获取供应商列表
    getSupplierList(supplierName, sendState, createDate, createUserId) {
      if (sendState || sendState === 0) {
        this.sendStateTag = 1;
      } else {
        this.sendStateTag = 0;
      }
      if (createUserId || createUserId === 0) {
        this.createUserIdState = 1;
      } else {
        this.createUserIdState = 0;
      }
      let sendState2 = "";
      sendState2 = String(sendState);
      getSupplierList({
        supplierName,
        sendState: sendState2,
        createDate,
        createUserId
      }).then(res => {
        console.log(res.data, "供应商列表");
        if (res.data.data && res.data.data.length) {
          res.data.data.map(item => {
            if (item.supplier_tel) {
              item.supplier_tel2 =
                item.supplier_tel.substr(0, 3) +
                "****" +
                item.supplier_tel.substr(7);
            }
          });
        }
        this.SupplierList = res.data.data;
        if (this.SupplierList.length == 0) {
          this.state1 = 1;
        } else {
          this.state1 = 0;
        }
        this.refreshing = false;
      });
    },
    // 客户列表
    getCustomerList(customerName, sendState, createDate, createUserId) {
      let sendState2 = "";
      sendState2 = String(sendState);
      getCustomerList({
        customerName,
        sendState: sendState2,
        createDate,
        createUserId
      }).then(res => {
        console.log(res.data, "客户列表");
        if (sendState || sendState === 0) {
          this.sendStateTag = 1;
        } else if (sendState !== 0 && !sendState) {
          this.sendStateTag = 0;
        }
        if (createUserId || createUserId === 0) {
          this.createUserIdState = 1;
        } else {
          this.createUserIdState = 0;
        }
        if (res.data.data && res.data.data.length) {
          res.data.data.map(item => {
            if (item.customer_tel) {
              item.customer_tel2 =
                item.customer_tel.substr(0, 3) +
                "****" +
                item.customer_tel.substr(7);
            }
          });
        }
        this.CustomerList = res.data.data;
        if (this.CustomerList.length == 0) {
          this.state2 = 1;
        } else {
          this.state2 = 0;
        }
        this.refreshing2 = false;
      });
    },
    // 获取创建人下拉列表
    getCreateUserOption() {
      getCreateUserOption().then(res => {
        let arr = [];
        arr.push({ name: "全部", value: "" });
        res.data.data.map(item => {
          arr.push({ name: item.username, value: item.user_id });
        });
        console.log(res.data.data, "创建人列表");
        this.actions = arr;
      });
    },
    // 获取用户信息
    findUserInfo() {
      findUserInfo().then();
    },
    onRefresh() {
      this.getSupplierList(
        this.supplierName,
        this.orderValue,
        this.date,
        this.user_id
      );
    },
    onRefresh2() {
      this.getCustomerList(
        this.supplierName,
        this.orderValue,
        this.date,
        this.user_id
      );
    }
  }
};
</script>